package testgenerator;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ImageViewer.java
 *
 * Created on 31/03/2010, 11:57:05 AM
 */
//import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
//import java.util.Collection;

/**
 *
 * @author stussy
 */
public class ImageViewer extends javax.swing.JFrame {

    /** Creates new form ImageViewer */
    public ImageViewer() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jFileChooser1 = new javax.swing.JFileChooser();
        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        txtSize1 = new javax.swing.JTextField();
        txtSize2 = new javax.swing.JTextField();
        txtSize3 = new javax.swing.JTextField();
        txtRatio1 = new javax.swing.JTextField();
        txtRatio2 = new javax.swing.JTextField();
        txtRatio3 = new javax.swing.JTextField();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txtGap = new javax.swing.JTextField();

        jFileChooser1.setCurrentDirectory(new java.io.File("/home/stussy/Desktop/NiSee/Repository/Source/temp"));
        jFileChooser1.setMultiSelectionEnabled(true);
        jFileChooser1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFileChooser1ActionPerformed(evt);
            }
        });
        jDialog1.getContentPane().add(jFileChooser1, java.awt.BorderLayout.CENTER);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(655, 400));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel4.setLayout(new java.awt.CardLayout());

        jButton3.setText("Add Images");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel5.add(jButton3);

        jPanel4.add(jPanel5, "card3");

        jPanel1.setLayout(new java.awt.GridLayout(1, 2));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/blank.JPG"))); // NOI18N
        jLabel1.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/blank.JPG"))); // NOI18N
        jPanel2.add(jLabel1);

        jButton2.setText("Previous");
        jButton2.setEnabled(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton2);

        jButton1.setText("Next");
        jButton1.setEnabled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1);

        jPanel1.add(jPanel2);

        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtSize1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSize1ActionPerformed(evt);
            }
        });
        jPanel3.add(txtSize1, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 40, 60, -1));
        jPanel3.add(txtSize2, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 40, 60, -1));
        jPanel3.add(txtSize3, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 40, 60, -1));
        jPanel3.add(txtRatio1, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 80, 60, -1));
        jPanel3.add(txtRatio2, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 80, 60, -1));
        jPanel3.add(txtRatio3, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 80, 60, -1));

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("Circle");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });
        jPanel3.add(jRadioButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 170, -1, -1));

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Square");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });
        jPanel3.add(jRadioButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 170, -1, -1));

        jLabel2.setText("Size");
        jPanel3.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 40, -1, -1));

        jLabel3.setText("Ratio");
        jPanel3.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 80, -1, -1));

        jButton5.setText("Refresh");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 230, -1, -1));

        jLabel4.setText("Gap");
        jPanel3.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 120, -1, -1));
        jPanel3.add(txtGap, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 120, 60, -1));

        jPanel1.add(jPanel3);

        defaultSizes = new ArrayList();
        defaultSizes.add(15);
        defaultSizes.add(10);
        defaultSizes.add(5);
        defaultPercentages = new ArrayList();
        defaultPercentages.add(20);
        defaultPercentages.add(27);
        defaultPercentages.add(29);

        jPanel4.add(jPanel1, "card2");

        getContentPane().add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, 590, 360));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        current++;
        if (current < 1 || current > MAX) {
            current = 1;
        }

        /*ArrayList foreground = gen.FillForeground(providedImages[current - 1], defaultFormat, defaultPercentages, defaultSizes, 1);
        int x = 0;
        gen.CreateImage(current, "TestUser", foreground, defaultFormat, defaultPercentages, defaultSizes, 1, new Color(colourset[x][1][0], colourset[x][1][1], colourset[x][1][2]), new Color(colourset[x][0][0], colourset[x][0][1], colourset[x][0][2]));
         */
        txtSize1.setText(defaultSizes.get(0) + "");
        txtSize2.setText(defaultSizes.get(1) + "");
        txtSize3.setText(defaultSizes.get(2) + "");
        txtRatio1.setText(defaultPercentages.get(0) + "");
        txtRatio2.setText(defaultPercentages.get(1) + "");
        txtRatio3.setText(defaultPercentages.get(2) + "");
        txtGap.setText(defaultGap + "");
        jRadioButton2.setSelected(true);
        if (!generated[current - 1]) {
            refreshImage();
            generated[current - 1] = true;
        }
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/testUser/" + current + "_out.JPG")));
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        current--;
        if (current < 1 || current > MAX) {
            current = MAX;
        }

        /*ArrayList foreground = gen.FillForeground(providedImages[current - 1], defaultFormat, defaultPercentages, defaultSizes, 1);
        int x = 0;
        gen.CreateImage(current, "TestUser", foreground, defaultFormat, defaultPercentages, defaultSizes, 1, new Color(colourset[x][1][0], colourset[x][1][1], colourset[x][1][2]), new Color(colourset[x][0][0], colourset[x][0][1], colourset[x][0][2]));
         */
        txtSize1.setText(defaultSizes.get(0) + "");
        txtSize2.setText(defaultSizes.get(1) + "");
        txtSize3.setText(defaultSizes.get(2) + "");
        txtRatio1.setText(defaultPercentages.get(0) + "");
        txtRatio2.setText(defaultPercentages.get(1) + "");
        txtRatio3.setText(defaultPercentages.get(2) + "");
        txtGap.setText(defaultGap + "");
        jRadioButton2.setSelected(true);

        if (!generated[current - 1]) {
            refreshImage();
            generated[current - 1] = true;
        }
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/testUser/" + current + "_out.JPG")));
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        jDialog1.setSize(425, 425);
        jDialog1.setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jFileChooser1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFileChooser1ActionPerformed
        // TODO add your handling code here:
        providedImages = jFileChooser1.getSelectedFiles();
        MAX = providedImages.length;
        images = new niImage[MAX];
        generated = new boolean[MAX];
        for (int i = 0; i < MAX; i++) {
            System.out.println(providedImages[i].toString());
        }
        jDialog1.setVisible(false);
        jButton3.setEnabled(false);
        jButton5.setEnabled(true);

        CardLayout cl = (CardLayout) (jPanel4.getLayout());
        cl.next(jPanel4);
        txtSize1.setText(defaultSizes.get(0) + "");
        txtSize2.setText(defaultSizes.get(1) + "");
        txtSize3.setText(defaultSizes.get(2) + "");
        txtRatio1.setText(defaultPercentages.get(0) + "");
        txtRatio2.setText(defaultPercentages.get(1) + "");
        txtRatio3.setText(defaultPercentages.get(2) + "");
        txtGap.setText(defaultGap + "");
        jRadioButton2.setSelected(true);
        refreshImage();
        generated[current - 1] = true;
        jButton3.setVisible(false);
    }//GEN-LAST:event_jFileChooser1ActionPerformed

    private void txtSize1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSize1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtSize1ActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        refreshImage();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void refreshImage() {
        // TODO add your handling code here:

        String username = "testUser";
        File f = new File(username);
        try {
            if (f.mkdir()) {
                System.out.println(username + " directory created");
            } else {
                System.out.println(username + " directory is not created");
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        File tempf = new File(username + "/" + current + "_out.JPG");
        if (tempf.exists()) {
            tempf.delete();
        }
        jLabel1.setIcon(null);
        //Administrator could do something like this:
        ArrayList percentages = new ArrayList();
        percentages.add(Integer.parseInt(txtRatio1.getText()));
        percentages.add(Integer.parseInt(txtRatio2.getText()));
        percentages.add(Integer.parseInt(txtRatio3.getText()));
        ArrayList sizes = new ArrayList();
        sizes.add(Integer.parseInt(txtSize1.getText()));
        sizes.add(Integer.parseInt(txtSize2.getText()));
        sizes.add(Integer.parseInt(txtSize3.getText()));
        /*for (int i = 0; i < 3; i++) {
        System.out.println("Size " + i + ": " + sizes.get(i));
        System.out.println("Perc " + i + ": " + percentages.get(i));
        }*/
        int gap = Integer.parseInt(txtGap.getText());
        int format;
        if (jRadioButton1.isSelected()) {
            format = 2;
        } else {
            format = 1;
        }

        //ArrayList foreground = gen.FillForeground(providedImages[current - 1], format, percentages, sizes, gap);

        int numberOfForegrounds = 1;
        if (gen.FillForeground(providedImages[current - 1], format, percentages, sizes, gap, numberOfForegrounds)) {
            BufferedImage bf = gen.CreateImage(gen.foreground1, gen.foreground2, format, percentages, sizes, gap, Color.red, Color.green, Color.black);
            //BufferedImage bf = gen.CreateImage(foreground1, null, format, percentages, sizes, gap, new Color(colourset[x][1][0], colourset[x][1][1], colourset[x][1][2]),null, new Color(colourset[x][0][0], colourset[x][0][1], colourset[x][0][2]));
            File theImg = new File(username + "/" + current + "_out.JPG");
            try {
                ImageIO.write(bf, "JPG", theImg);
            } catch (IOException e) {
            }



            //gen.CreateTestForegrounds(providedImages, percentages, sizes, gap, format);


            //Student could do something like this:
            //gen.CreateTest("Test1", "testUser");

            //System.out.println("Finished");
            //jLabel2.setIcon(new javax.swing.ImageIcon(providedImages[current-1].toString()));


            jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/" + username + "/" + current + "_out.JPG")));
            jButton1.setEnabled(true);
            jButton2.setEnabled(true);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                new ImageViewer().setVisible(true);
            }
        });
    }
    //{183, 180, 128}
    int colourset[][][] = {{{130, 170, 150}, {249, 128, 93}}, {{183, 180, 128}, {236, 170, 141}}, {{138, 129, 125}, {226, 137, 128}}};
    private ArrayList defaultSizes;// (15, 10, 5);
    private ArrayList defaultPercentages;// = {20, 27, 29};
    private int defaultFormat = 2;
    private int defaultGap = 0;
    private static int current = 1;
    private static int MAX = 0;//number of images
    private static File providedImages[];
    private TestGenerator gen = new TestGenerator();
    niImage images[];
    boolean generated[];
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton5;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JTextField txtGap;
    private javax.swing.JTextField txtRatio1;
    private javax.swing.JTextField txtRatio2;
    private javax.swing.JTextField txtRatio3;
    private javax.swing.JTextField txtSize1;
    private javax.swing.JTextField txtSize2;
    private javax.swing.JTextField txtSize3;
    // End of variables declaration//GEN-END:variables
}
